package com.life.ai.server.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.life.ai.server.common.domain.PageQuery;
import com.life.ai.server.common.domain.R;
import com.life.ai.server.common.utils.ValidatorUtil;
import com.life.ai.server.framework.syslog.annotation.SysLog;
import com.life.ai.server.modules.system.entity.SysUser;
import com.life.ai.server.modules.system.model.dto.ModifyPwdDTO;
import com.life.ai.server.modules.system.model.dto.SysUserDTO;
import com.life.ai.server.modules.system.model.dto.UpdateStatusDTO;
import com.life.ai.server.modules.system.model.dto.UserQueryDTO;
import com.life.ai.server.modules.system.model.vo.SysUserVO;
import com.life.ai.server.modules.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息表 前端控制器
 *
 * @author kyle
 * @since  2024-06-14
 */
@Api(tags = "用户信息表-接口列表")
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 用户信息表-单条查询
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "单条查询")
    @GetMapping("/{id}")
    public R<SysUserVO> queryOne(@PathVariable("id") Long id) {
        SysUser sysUser = sysUserService.queryOne(id);
        return R.ok(BeanUtil.copyProperties(sysUser, SysUserVO.class));
    }

    /**
     * 用户信息表-分页查询
     */
    @SysLog("查询用户列表")
    @SaCheckPermission("system:user:list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public R<Page<SysUserVO>> queryPage(PageQuery pageQuery, UserQueryDTO userQuery) {
        Page<SysUserVO> page = sysUserService.queryPage(pageQuery, userQuery);
        return R.ok(page);
    }

    /**
     * 用户信息表-新增
     */
    @SysLog("新增用户")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody SysUserDTO sysUserDTO) {
        sysUserService.addUser(sysUserDTO);
        return R.ok();
    }

    /**
     * 用户信息表-修改
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public R<Void> update(@Validated(ValidatorUtil.Update.class) @RequestBody SysUserDTO sysUserDTO) {
        sysUserService.updateUser(sysUserDTO);
        return R.ok();
    }

    /**
     * 用户信息表-禁/启用
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量修改用户状态（0-正常 1-停用）")
    @PutMapping("/updateStatus")
    public R<Void> updateStatus(@RequestBody UpdateStatusDTO updateStatusDTO) {
        sysUserService.updateStatus(updateStatusDTO);
        return R.ok();
    }


    /**
     * 用户信息表-删除
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public R<Void> delete(@PathVariable("id") Long id) {
        sysUserService.delete(id);
        return R.ok();
    }

    /**
     * 用户信息表-批量删除
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "批量删除")
    @PostMapping("/deleteBatch")
    public R<Void> deleteBatch(@RequestBody List<Long> list) {
        sysUserService.deleteBatch(list);
        return R.ok();
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "重置密码")
    @GetMapping("/resetPwd/{id}")
    public R<Void> resetPwd(@PathVariable("id") Long id) {
        sysUserService.resetPwd(id);
        return R.ok();
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "修改密码")
    @PostMapping("/modifyPwd")
    public R<Void> modifyPwd(@RequestBody @Validated ModifyPwdDTO modifyPwdDTO) {
        sysUserService.modifyPwd(modifyPwdDTO);
        return R.ok();
    }

}